﻿(function ($)
{
    $.user = {};

    $.user.login = {};

    $.user.login.page = null;

    $.user.verifyUser = function ()
    {
        $.session('user', function (data)
        {
            //Session
            if (data.user != null && data.user.Id > 0)
            {
                $.elements.loadElements();
            }
            else
            {
                var email = $.cookie('Email')
                var pass = $.cookie('Password')

                if (email != null && pass != null)
                {
                    var post = "txtUser=" + email + "&txtPass=" + pass;
                    $.user.login.post(post, function (data)
                    {
                        //Cookie User
                        if (data.sucess)
                        {
                            $.elements.loadElements();
                        }
                        else
                        {
                            $.user.login.showLogin();
                        }
                    });
                }
                else
                    $.user.login.showLogin();
            }
        });
    }

    $.user.login.init = function (callback)
    {
        $.post("login.html", null, function (data)
        {
            $.user.login.page = $(data);

            var $f = $.user.login.page;

            $f.dialog(
			 {
			     draggable: false,
			     width: '450',
			     modal: true,
			     resizable: false,
			     title: 'Welcome',
			     autoOpen: false,
			     close: $.user.login.verifyAndClose,
			     resize: 'auto'
			 });

            $("#btnSignup").click($.user.login.signUp);

            $("#btnLogin").click(function () { $.user.login.post(); });

            $("#btnLetMePLay").click($.user.login.guest);

            $("#btnLogin").button('option', 'label', 'Login').addClass("ui-state-hover");

            $("#btnLogin").hover(
			    function ()
			    {
			        $(this).addClass("ui-state-hover");
			    },
			    function ()
			    {
			        $(this).addClass("ui-state-hover");
			    });

            $("input:text, input:password, input:checkbox").keypress(function (e)
            {
                if (e.which == 13)
                {
                    $('#btnLogin').click();
                    e.preventDefault();
                }
            });

            $(".button").button().disableSelection();

            if (typeof (callback) == 'function')
                callback();

        }, "html");
    }

    $.user.login.guest = function ()
    {
        $.user.login.post("guest=1");
    }

    $.user.login.verifyAndClose = function ()
    {
        if ($.cookie('Email') == null)
            $.user.login.guest();
        else
        {
            //todo:call index\initialize
            $.elements.loadElements();

            $.news.loadNewElements('clear');

            $("#welcomeMessage").attr('closed', true);
        }
    }

    $.user.login.close = function ()
    {
        $("#loginMessage").dialog('close');
    }

    $.user.login.signUp = function ()
    {
        if ($("#btnSignup").text() == "Sign up")
        {
            $.user.login.showSignup();
        }
        else
        {
            $.user.login.showLogin();
        }
    }

    $.user.login.post = function (data, callback)
    {
        if (data == null)
            data = $("form").serialize();

        if (typeof (callback) != 'function')
        {
            callback = function (data)
            {
                if (data.sucess)
                {
                    $.user.login.close();
                }
                else
                {
                    $("#errorMessage").html(data.msg);

                    $("#errorMessage").fadeIn('fast');
                }

            }
        }

        $.post("./bus/login.php", data, callback);
    }

    $.user.login.showLogin = function ()
    {
        $("input").val("");

        if ($.user.login.page == null)
            $.user.login.init($.user.login.openForLogin);
        else
            $.user.login.openForLogin();
    }

    $.user.login.openForLogin = function ()
    {
        $("#hdnSignup").val(0);

        $("#btnSignup").button('option', 'label', 'Sign up');

        if ($("#loginMessage").dialog('isOpen'))
        {
            dialog = $("#loginMessage").closest('.ui-dialog');
            dialog.animate({ top: (document.height / 2) - (dialog.height() / 2) + ($("#retypePassword").height() / 2) }, 'fast');

            $("#retypePassword").verficalHide();

            $("#btnLogin").button('option', 'label', 'Login');
            $("#btnRecoverPassword").verficalShow();
        }
        else
        {
            $("#loginMessage").attr('closed', false);
            $("#loginMessage").dialog('open');
        }
    }


    $.user.login.showSignup = function ()
    {

        $("#hdnSignup").val(1);

        $("#btnLogin").button('option', 'label', 'Sign up');

        $("#btnSignup").button('option', 'label', 'Back to Login');

        dialog = $("#loginMessage").closest('.ui-dialog');
        dialog.animate({ top: (document.height / 2) - (dialog.height() / 2) - 40 }, 'fast');

        $("#retypePassword").verficalShow();
        $("#btnRecoverPassword").verficalHide();



        //$("#welcomeMessage").verticalShow();
    }


})(jQuery);
